﻿#region 命名空间

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Web;
using WinnerCMS.Common;
using WinnerCMS.Model.Attribute;
using WinnerCMS.WebFactory;

#endregion

namespace WinnerCMS.WebUI.Admin.ControlPanel
{
    /// <summary>
    ///     服务器探针
    /// </summary>
    [Access]
    public partial class ServerProbe : PageBase
    {
        [DllImport("kernel32")]
        public static extern void GlobalMemoryStatus(ref MEMORY_INFO meminfo);

        protected void Page_Load(object sender, EventArgs e)
        {
            //当Access数据库不存在的时候，用户名需要打开这个页面看看服务器的物理路径
            //Session["ComeIn"]这个Session只有在发生错误的时候生成
            if (Session["ComeIn"] == null)
            {
            }


            if (!(IsPostBack))
            {
                var myCommon = new HttpServer();
                var stime = DateTime.Now.TimeOfDay;
                this.servername.Text = Server.MachineName;
                this.serverms.Text = Environment.OSVersion.ToString();
                this.serverip.Text = Request.ServerVariables["LOCAL_ADDR"];
                this.server_name.Text = Request.ServerVariables["http_host"];
                this.serversoft.Text = Request.ServerVariables["server_software"];
                this.servernet.Text = Environment.Version.Major + "." + Environment.Version.Minor + "." +
                                      Environment.Version.Build + "." + Environment.Version.Revision;
                this.serverhttps.Text = Request.ServerVariables["HTTPS"];
                this.serverport.Text = Request.ServerVariables["server_port"];
                this.serverout.Text = Server.ScriptTimeout.ToString();
                this.servertime.Text = DateTime.Now.ToString();
                this.serverarea.Text = (DateTime.Now - DateTime.UtcNow).TotalHours > 0
                    ? "+" + (DateTime.Now - DateTime.UtcNow).TotalHours
                    : (DateTime.Now - DateTime.UtcNow).TotalHours.ToString();
                this.lblCache.Text = HttpRuntime.Cache.Count.ToString();
                try
                {
                    this.aspnetn.Text =
                        (Process.GetCurrentProcess().WorkingSet64/1048576).ToString("N2") + " MB";
                }
                catch
                {
                    this.aspnetn.Text = "系统拒绝提供。";
                }
                try
                {
                    this.aspnetcpu.Text =
                        (Process.GetCurrentProcess().TotalProcessorTime).TotalSeconds.ToString("N0") +
                        " 秒";
                }
                catch
                {
                    this.aspnetcpu.Text = "系统拒绝提供。";
                }
                this.serverstart.Text = (Environment.TickCount/3600000).ToString("N2");
                try
                {
                    this.prstart.Text = Process.GetCurrentProcess().StartTime.ToString();
                }
                catch
                {
                    this.prstart.Text = "系统拒绝提供。";
                }
                this.cpuc.Text = Environment.GetEnvironmentVariable("NUMBER_OF_PROCESSORS");
                this.cputype.Text = Environment.GetEnvironmentVariable("PROCESSOR_IDENTIFIER");
                if (myCommon.CheckObject("ADODB.RecordSet"))
                {
                    this.serveraccess.Text = "支持";
                }
                else
                {
                    this.serveraccess.Text = "不支持";
                }
                if (myCommon.CheckObject("Scripting.FileSystemObject"))
                {
                    this.serverfso.Text = "支持";
                }
                else
                {
                    this.serverfso.Text = "不支持";
                }
                if (myCommon.CheckObject("JMail.SmtpMail"))
                {
                    this.jmail.Text = "支持";
                }
                else
                {
                    this.jmail.Text = "不支持";
                }

                //调用GlobalMemoryStatus函数获取内存的相关信息
                MEMORY_INFO MemInfo;
                MemInfo = new MEMORY_INFO();
                GlobalMemoryStatus(ref MemInfo);
                this.LbdwMemoryLoad.Text = MemInfo.dwMemoryLoad + " %";
                this.LbdwTotalPhys.Text = FileIO.FileSize(MemInfo.dwTotalPhys);
                this.LbdwAvailPhys.Text = FileIO.FileSize(MemInfo.dwAvailPhys);
                this.LbdwTotalPageFile.Text = FileIO.FileSize(MemInfo.dwTotalPageFile);
                this.LbdwAvailPageFile.Text = FileIO.FileSize(MemInfo.dwAvailPageFile);
                this.LbdwTotalVirtual.Text = FileIO.FileSize(MemInfo.dwTotalVirtual);


                this.serverppath.Text = Request.ServerVariables["APPL_PHYSICAL_PATH"];
                this.servernpath.Text = Request.ServerVariables["path_translated"];
                this.servers.Text = Session.Contents.Count.ToString();
                this.servera.Text = Application.Contents.Count.ToString();
                var etime = DateTime.Now.TimeOfDay;
                var t = etime.Subtract(stime);
                this.runtime.Text = t.TotalMilliseconds.ToString();

                if (myCommon.CheckObject("MSWC.AdRotator"))
                {
                    this.lblAdRotator.Text = "支持";
                }
                else
                {
                    this.lblAdRotator.Text = "不支持";
                }

                if (myCommon.CheckObject("MSWC.BrowserType"))
                {
                    this.lblBrowserType.Text = "支持";
                }
                else
                {
                    this.lblBrowserType.Text = "不支持";
                }
                if (myCommon.CheckObject("MSWC.NextLink"))
                {
                    this.lblNextLink.Text = "支持";
                }
                else
                {
                    this.lblNextLink.Text = "不支持";
                }
            }
        }

        protected void Testbtn_Click(object sender, EventArgs e)
        {
            var t1 = DateTime.Now.TimeOfDay;

            for (var i = 0; i < 100000000; i++)
            {
                i += 1;
            }
            var t2 = DateTime.Now.TimeOfDay;
            var t3 = t2.Subtract(t1);
            this.showtest.Text = t3.TotalSeconds + " 秒";
        }

        protected void btnTest_Click(object sender, EventArgs e)
        {
            var myCommonClass = new HttpServer();
            if (myCommonClass.CheckObject(this.txtObjName.Text.Trim()))
            {
                MsgBox.Message("支持", this);
            }
            else
            {
                MsgBox.Message("不支持", this);
            }
        }

        //定义内存的信息结构
        [StructLayout(LayoutKind.Sequential)]
        public struct MEMORY_INFO
        {
            public uint dwLength;
            public uint dwMemoryLoad;
            public uint dwTotalPhys;
            public uint dwAvailPhys;
            public uint dwTotalPageFile;
            public uint dwAvailPageFile;
            public uint dwTotalVirtual;
            public uint dwAvailVirtual;
        }
    }
}